require(['./config'],()=>{
    require(['jquery'],($)=>{
        $('header').load('./common/header.html')
        $('footer').load('./common/footer.html')
        class ShopList{
            constructor(){
                this.proBox = $('.pro-list-all')[0]
                this.addBtn = $('.btn')[0]
                this.init()
            }
            init(){
                // this.addGoods()
                this.request()
                this.tz()
                // this.singleGoodsMoney()
            }
            request(){
                $.get('http://api.yuguoxy.com/api/shop/list', {pageSize:1000,pageNo:1},'json')
                .then(res=>{
                    var data = res.resultInfo.list
                    this.render(data)
                    // console.log(data);
                })
            }
            render(data){
                data.forEach(item=>{
                this.proBox.innerHTML  += `
                <ul>
                    <li class="pro-img"><img src="${item.picture}" alt=""></li>
                    <li class="pro-name">${item.product}</li>
                    <li class="pro-price">${item.price}元</li>
                    <li class="pro-label">原价为${item.oldprice}元</li>
                    <li class="pro-comment">${item.categoryname}</li>
                    <li class="btn" data-id=${item.id}>加入购物车</li>
                </ul>
                `
                     })
                this.addGoods(data)
            }
            addGoods(data){
                $('.pro-list-all>ul').on('click',(e)=>{
                    // console.log(data);
                    let target = e.target || e.srcElement
                    if(target.className != 'btn'){
                        window.location.href = '/html/details.html'
                    }
                    if(target.className == 'btn'){
                        let id = target.dataset.id-0
                        let goods = data.find(item=>{return item.id == id})
                        // console.log(goods);
                        let list = JSON.parse(localStorage.getItem('cart')) || []
                        if(!list.length){
                            list.push(goods)
                            goods.cart_number = 1
                        }else{
                            let res = list.some(item=>{return item.id == id})
                            if(res){
                                let index = list.findIndex(item=>{return item.id == id})
                                list[index].cart_number ++
                            }else{
                                list.push(goods)
                                goods.cart_number = 1
                            }
                        }
                        localStorage.setItem('cart',JSON.stringify(list))
                    }
                })
            }
            tz(){
                $('.icon-gouwuche').on('click',()=>{
                    window.location.href = '/html/cart.html'
                })
                $('.icon-kefu').on('click',()=>{
                    window.location.href = '/html/kf.html'
                })
            }
        }
        new ShopList()
    })
})

